<?php
namespace haibao\cms\business;
use haibao\cms\data\FeatureSerial as FeatureSerialData;
use haibao\cms\model\filter\FeatureSerial as FeatureSerialFilter;
use haibao\cms\model\data\FeatureSerial as FeatureSerialModel;
use haibao\cms\model\filter\FeaturePublish as FeaturePublishFilter;
use haibao\frame\data\query\Condition;
use haibao\cms\model\data\FeaturePublish as FeaturePublishModel;
use haibao\cms\business\FeaturePublish as FeaturePublishBusiness;

class FeatureSerial extends \haibao\cms\business\Base{
    
    public static function getSerialIdByName($serialName){
        $data = new \haibao\cms\data\FeatureSerial();
        return $data->getSerialIdByName($serialName);
    }
    
	public static function getMonths($serialId){
		$data = new \haibao\cms\data\FeatureSerial();
		
		return $data->getMonths($serialId);
	}
	
	public static function delSerial($id){
		self::checkOperation(self::FUNC_CODE_FEATURE_SERIAL_DEL);
		if (!$id){
			throw new BusinessException('系列Id不能为空',self::DATA_EMPTY_FEATURE_SERIAL_ID);
		}
		
		$filter = new FeaturePublishFilter();
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_SERIAL_ID,Condition::CONDITION_EQUAL,$id);
		$filter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,FeaturePublishModel::STATUS_DISABLE);
		if (FeaturePublishBusiness::searchFeature($filter)){
			throw new BusinessException('该系列下还有专题');
		}
		
		$data = new FeatureSerialData();
		$arrSet = array(
			\haibao\cms\model\filter\FeatureSerial::CONDITION_FIELD_NAME_STATUS=>\haibao\cms\model\data\FeatureSerial::STATUS_DISABLE
		);
		$data->editSerial($id, $arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_SERIAL_DEL, $id);
	}
	
	/**
	 * @param \haibao\cms\model\filter\FeatureSerial $model
	 */
	public static function getAllSerials($model){
		$data = new FeatureSerialData();
		
		return $data->getAll($model);
	}
	
	public static function addSerial($model){
		self::checkOperation(self::FUNC_CODE_FEATURE_SERIAL_ADD);
		if (!$model->SerialName){
			throw new BusinessException('系列名称不能为空',self::DATA_EMPTY_FEATURE_SERIAL_NAME);
		}
		$filter = new FeatureSerialFilter();
		$filter->where(FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_NAME,Condition::CONDITION_EQUAL,$model->SerialName);
		$filter->where(FeatureSerialFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,FeatureSerialModel::STATUS_ENABLE);
		if (self::getAllSerials($filter)){
			throw new BusinessException('系列名称重复',self::DATA_ERROR_FEATURE_SERIAL_NAME_EXIST);
		}
		
		$model->CreateTime = new \DateTime();
		$model->CreateBy = self::getCurrentUser()->Id;
		$model->Status = FeatureSerialModel::STATUS_ENABLE;
		$data = new FeatureSerialData();
		$data->addSerial($model);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_SERIAL_ADD, $model->toArray());
	}
	
	public static function editSerial($id,$arrSet){
		self::checkOperation(self::FUNC_CODE_FEATURE_SERIAL_EDIT);
		if (isset($arrSet[FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_NAME]) && empty($arrSet[FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_NAME])){
			throw new BusinessException('系列名称不能为空',self::DATA_EMPTY_FEATURE_SERIAL_NAME);
		}
		
		if (isset($arrSet[FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_NAME])){
			$name = $arrSet[FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_NAME];
			$filter = new FeatureSerialFilter();
			$filter->where(FeatureSerialFilter::CONDITION_FIELD_NAME_SERIAL_NAME,Condition::CONDITION_EQUAL,$name);
			$filter->where(FeatureSerialFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,FeatureSerialModel::STATUS_ENABLE);
			$filter->where(FeatureSerialFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTEQUAL,$id);
			if (self::getAllSerials($filter)){
				throw new BusinessException('系列名称重复',self::DATA_ERROR_FEATURE_SERIAL_NAME_EXIST);
			}
		}
		
		$data = new FeatureSerialData();
		$data->editSerial($id, $arrSet);
		
		self::saveOperateHistory(self::FUNC_CODE_FEATURE_SERIAL_EDIT, array_merge($arrSet,array('id'=>$id)));
	}
	
	/**
	 * 专题树形结构
	 */
	public static function getCategoryTree(){
		$videoCategoryData = new FeatureSerialData();
		
		return $videoCategoryData->getCategoryTree();
	}
	
}